//
//  KLFindPasswordDetailViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/4/25.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLFindPasswordDetailViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "JRToast.h"

@interface KLFindPasswordDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdNew;
@property (weak, nonatomic) IBOutlet UITextField *pwdNewAgain;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@end

@implementation KLFindPasswordDetailViewController

-(void)viewWillAppear:(BOOL)animated{

    self.navigationItem.title = @"找回密码详情";
    
    //左侧返回按钮
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.submitBtn setBackgroundColor:[KLHelp mainBlueColor]];
    self.submitBtn.layer.cornerRadius = 5;
    [self.submitBtn setTitle:@"提     交" forState:UIControlStateNormal];
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.submitBtn.titleLabel setFont:[UIFont systemFontOfSize:19]];
}

- (IBAction)submit:(UIButton *)sender {
    NSString *passwordNew = self.pwdNew.text;
    NSString *passwordAgain = self.pwdNewAgain.text;
    NSString *temp_token = [[NSUserDefaults standardUserDefaults] objectForKey:@"temp_token"];
    
    if(passwordNew.length < 6){
        [KLHelp showTextHUDforView:self.view text:@"新密码不能低于6位"];
        return ;
    }else if(passwordAgain.length < 6){
        [KLHelp showTextHUDforView:self.view text:@"重复新密码不能低于6位"];
        return ;
    }else if(![passwordNew isEqualToString:passwordAgain]){
        [KLHelp showTextHUDforView:self.view text:@"两次输入密码不一致"];
        return ;
    }
    
    [KLHelp showHUDAddedTo:self.view title:@"请稍候..." details:nil animated:YES];
    [KLHttpAPI findBackPasswordWithKeyType:@"1" new_password:passwordNew temp_token:temp_token completionHandler:^(NSString *responseString) {
        NSLog(@"返回成功:%@",responseString);
        NSDictionary *result = [KLHelp dictionaryWithJsonString:responseString];
        NSString *isSuccess = [result objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:passwordNew forKey:@"password"];
            
            [KLHelp hideAllHUDsForView:self.view animated:YES];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [JRToast showWithText:[result objectForKey:@"msg"]];
            [KLHelp hideAllHUDsForView:self.view animated:YES];
        }
    } errorHandler:^(NSString *error) {
        [KLHelp hideAllHUDsForView:self.view animated:YES];
        [JRToast showWithText:error];
    }];
}

#pragma mark -方法中的方法体
-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -取消view的第一响应者身份
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
